package com.storage.joyce.storageapp.util;




import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import com.google.gson.Gson;
import com.storage.joyce.storageapp.util.view.LoadingDialog;
import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpConfig;
import org.kymjs.kjframe.http.HttpParams;



import java.util.Map;


/**
 * Created by john on 2016/2/22.
 */
public class HttpService {

    private final static KJHttp kjhttp;
    public final static Gson gson = new Gson();
    public static String token;//用户token
    //配送员app
    public static  String login_url = ServerAddress.serviceAdress + "/index/login";
    public static  String daipeisong_url = ServerAddress.serviceAdress + "/order/orderList";
    public static  String peisongzhong_url = ServerAddress.serviceAdress + "/order/myOrder";
    public static  String yipeisong_url = ServerAddress.serviceAdress + "/order/completedOrder";
    public static  String changestatus_url = ServerAddress.serviceAdress + "/order/distribution";
    public static  String changestatus_url2 = ServerAddress.serviceAdress + "/order/changeState";
    public static  String changestatus_url3 = ServerAddress.serviceAdress + "/order/getOrder";

    static {
        HttpConfig.TIMEOUT = 3000;
        HttpConfig kjConfig = new HttpConfig();
        kjhttp = new KJHttp(kjConfig);
    }

    public static void post(String url, Map<String, Object> map, HttpCallBack callBack) {
        post1(url, map, callBack, false);
    }


    public static void cancle(String url) {
        kjhttp.cancel(url);
    }

    private static Dialog dialog;


    public static void post1(final String url, Map<String, Object> map, final HttpCallBack callBack, final boolean isLoading) {
        HttpParams params = new HttpParams();
        params.putHeaders("Content-Type", "application/json");
        params.putJsonParams(CommonJSONParser.MapToJson(map));
        if (null != token) {
            Log.e("##", token);
            params.putHeaders("token", token.replaceAll(";", ""));
        }
        Log.e("params+++++", params + "");
        params.putJsonParams(gson.toJson(map));
        Log.e("NetWork", gson.toJson(map) + "");
        kjhttp.jsonPost(url, params, false, new HttpCallBack() {
            @Override
            public void onPreStart() {
                super.onPreStart();
                if (isLoading) {
                    Tools.loge("345532525425++++" + ActivityUtil.getActivityContext());
                    dialog = LoadingDialog.createLoadingDialog(ActivityUtil.getActivityContext());
                    dialog.show();
                    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            cancle(url);
                            callBack.onFailure(-2, "取消操作");
                        }
                    });
                }
                callBack.onPreStart();
            }

            @Override
            public void onSuccess(String t) {
                super.onSuccess(t);
                callBack.onSuccess(t);
            }

            @Override
            public void onSuccess(Map<String, String> headers, byte[] t) {
                super.onSuccess(headers, t);
                callBack.onSuccess(headers, t);
            }

            @Override
            public void onSuccess(Bitmap t) {
                super.onSuccess(t);
                callBack.onSuccess(t);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                Tools.toast("服务器连接失败，请稍后再试");
                Tools.loge("失败原因：" + strMsg);
                if (isLoading) dialog.dismiss();
                callBack.onFailure(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (isLoading) dialog.dismiss();
                callBack.onFinish();
            }

            @Override
            public void onLoading(long count, long current) {
                super.onLoading(count, current);
                callBack.onLoading(count, current);
            }

        });
    }


    public static boolean isNetworkAvailable(Activity activity) {
        Context context = activity.getApplicationContext();
        // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        } else {
            // 获取NetworkInfo对象
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {
                    Tools.loge(i + "===状态===" + networkInfo[i].getState());
                    Tools.loge(i + "===类型===" + networkInfo[i].getTypeName());
                    // 判断当前网络状态是否为连接状态
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
